package com.ibeeking.gmc.global.g.rest.controller.v1;

import com.ibeeking.found.common.entity.*;
import com.ibeeking.gmc.global.api.common.dto.JsapiSignatureDTO;
import com.ibeeking.gmc.global.api.common.dto.TenantDTO;
import com.ibeeking.gmc.global.api.common.query.JsapiSignatureQuery;
import com.ibeeking.gmc.global.g.rest.service.*;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.open.OpenAuthQuery;
import com.ibeeking.found.common.entity.open.SimpleOpenAuthDTO;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.service.common.param.OpenConfigParam;
import com.ibeeking.gmc.global.service.common.query.OpenConfigPageQuery;
import com.ibeeking.gmc.global.service.common.vo.HasOpenAuthVO;
import com.ibeeking.gmc.global.service.common.vo.OpenConfigVO;
import com.ibeeking.gmc.global.service.common.vo.TenantVO;
import com.ibeeking.gmc.global.service.common.vo.WxOpenMaTemplateDraftVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ClientController控制器
 * @Description 授权客户端 全局端界面维护
 * @Author ibeeking
 * @Date 2021-05-18 10:34
 **/
@RestController
@RequestMapping("/g/open/v1")
public class OpenController {

    @Resource
    private ITenantService tenantServiceImpl;

    @Resource
    private ITenantDetailService tenantDetailServiceImpl;

    @Resource
    private IGlobalVariableService globalVariableServiceImpl;

    @Resource
    private ITenantVariableService tenantVariableServiceImpl;

    @Resource
    private IOpenConfigService wxOpenConfigServiceImpl;

    @Resource
    private IOpenAuthService wxOpenAuthServiceImpl;

    @Resource
    private IWxOpenService wxOpenServiceImpl;


    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询第三方平台配置配置列表")
    @PostMapping(value = "/searchOpenApiConfigList")
    public List<Map<String,Object>> searchOpenApiConfigList() {
        return wxOpenConfigServiceImpl.queryOpenApiTypeList();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询微信第三方平台配置")
    @PostMapping(value = "/pageList")
    public Page<OpenConfigVO> pageList(@RequestBody OpenConfigPageQuery groupPageQuery) {
        return this.wxOpenConfigServiceImpl.pageList(groupPageQuery);
    }

    @Log(logType = OperateLogTypeEnum.QUERY,describe = "根据id查询微信第三方平台配置")
    @PostMapping(value = "/view/{id}")
    public OpenConfigVO view(@PathVariable("id") Long id) {
        return this.wxOpenConfigServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增微信第三方平台配置")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) OpenConfigParam openConfigParam) {
        openConfigParam.setCreateBy(currentUser.getId());
        openConfigParam.setModifyBy(currentUser.getId());
        return wxOpenConfigServiceImpl.add(openConfigParam);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "更新微信第三方平台配置")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) OpenConfigParam openConfigParam) {
        openConfigParam.setModifyBy(currentUser.getId());
        return wxOpenConfigServiceImpl.modify(openConfigParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "配置发布状态")
    @PostMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return wxOpenConfigServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除微信第三方平台配置信息")
    @PostMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return wxOpenConfigServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询微信第三方平台是否已授权")
    @PostMapping("/queryHasOpenAuth")
    public HasOpenAuthVO queryHasWxOpenAuth() {
        return wxOpenAuthServiceImpl.queryHasOpenAuth(Integer.valueOf(UserUtils.getTenantId()));
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询微信第三方平台已授权应用")
    @PostMapping("/queryDefaultOpenAuth")
    public SimpleOpenAuthDTO querySimpleOpenAuthOne(@RequestBody OpenAuthQuery query) {
        return wxOpenAuthServiceImpl.querySimpleOpenAuthOne(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户详情查询")
    @PostMapping(value = "/queryTenantById/{id}")
    public TenantDTO queryTenantById(@PathVariable("id") Integer id) {
        return tenantServiceImpl.queryTenantById(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "租户详细介绍信息查询")
    @PostMapping(value = "/queryDetailByTenantId/{type}/{id}")
    public TenantDetailDTO queryDetailByTenantId(@PathVariable("type") Integer type, @PathVariable("id") Integer id) {
        return tenantDetailServiceImpl.queryDetailByTenantId(type, id);
    }

    //C端全局查tenantId
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "取得租户的信息")
    @PostMapping("/queryTenantIdByAppid/{appId}")
    public String queryTenantIdByAppid(@PathVariable("appId") String appId) {
        OpenAuthQuery query = new OpenAuthQuery();
        query.setAppId(appId);
        SimpleOpenAuthDTO openAuthDTO  = wxOpenAuthServiceImpl.querySimpleOpenAuthOne(query);
        if(openAuthDTO == null) return "";
        if(!openAuthDTO.getHasOpenAuth()) return "";
        return openAuthDTO.getTenantId().toString();
    }
    /**
     * 通过param查询全局参数值
     *
     * @param query
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询全局参数值")
    @PostMapping(value = "/queryGlobalVariable")
    public GlobalVariableDTO queryGlobalVariable(@RequestBody VariableQuery query) {
        return globalVariableServiceImpl.queryGlobalVariable(query);
    }

    /**
     * 通过param查询全局参数值
     *
     * @param query
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询全局参数值")
    @PostMapping(value = "/queryTenantVariable")
    public TenantVariableDTO queryTenantVariable(@RequestBody VariableQuery query) {
        return tenantVariableServiceImpl.queryTenantVariable(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询绑定小程序模板草稿列表")
    @PostMapping(value = "/getWxOpenMaTemplateDraftList/{id}")
    public List<WxOpenMaTemplateDraftVO> getWxOpenMaTemplateDraftList(@PathVariable("id") String configId) {
        return wxOpenConfigServiceImpl.getWxOpenMaTemplateDraftList(configId);
    }
    /**
     * JsApi签名
     * @param query
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "JsApi签名")
    @PostMapping(value = "/getJsapiSignature")
    public JsapiSignatureDTO getJsapiSignature(@RequestBody JsapiSignatureQuery query) {
        return wxOpenServiceImpl.createJsapiSignature(query);
    }

}
